﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CS_BEGINNER_TO_PROFICIENT.DesignPatterns.CreationMode.SingletonPattern
{
    // 单例双重锁
    public class SingleObjectDoubleLock
    {
        private static SingleObjectDoubleLock _instance;
        private static readonly object _lock = new object();

        private SingleObjectDoubleLock() { }

        public static SingleObjectDoubleLock GetInstance()
        {
            if (_instance == null)
            {
                lock (_lock)
                {
                    if (_instance == null)
                    {
                        _instance = new SingleObjectDoubleLock();
                    }
                }
            }
            return _instance;
        }

        public void ShowMessage()
        {
            Console.WriteLine("Hello World");
        }

        public static void Execute()
        {
            SingleObjectHungry ob = SingleObjectHungry.GetInstance();
            ob.ShowMessage();
        }
    }
}
